﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.DirectShow;
using System.Windows.Media.DirectShow.Filters;
using System.Windows.Media.DirectShow.UI;


namespace Test
{
    public partial class CapturePage : UserControl
    {
        private FilterGraph graph;
        private WaveWriter wavWriter;
        private FileWriter fileWriter;

        private DeviceCaptureSource sourceFilter;
        private AudioVisualization audioVisualization;

        public CapturePage()
        {
            InitializeComponent();

            graph = new FilterGraph();
            graph.MediaEvent += new EventHandler<MediaEventArgs>(graph_MediaEvent);

            sourceFilter = new DeviceCaptureSource();
            sourceFilter.LoadCompleted += new System.ComponentModel.AsyncCompletedEventHandler(sourceFilter_LoadCompleted);

            graph.AddFilter("source", sourceFilter);

            BarVisualization visualization = new BarVisualization();
            audioVisualization = new AudioVisualization(visualization);
            this.v.Child = audioVisualization.UIElement;
            graph.AddFilter("av", audioVisualization);

            wavWriter = new WaveWriter();
            graph.AddFilter("wav writer", wavWriter);

            fileWriter = new FileWriter();
            graph.AddFilter("file writer", fileWriter);
        }

        void sourceFilter_LoadCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            graph.ConnectFilters(sourceFilter, audioVisualization);
            graph.ConnectFilters(audioVisualization, wavWriter);
            graph.ConnectFilters(wavWriter, fileWriter);

            graph.RunAsync();
        }

        private void graph_MediaEvent(object sender, MediaEventArgs e)
        {
            switch (e.Notification)
            {
                case MediaNotification.Completed:
                    graph.StopAsync();
                    break;
                case MediaNotification.Stopped:
                    fileWriter.FileStream.Close();
                    MessageBox.Show("Done!");
                    break;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            graph.StopAsync();
        }

        private void Pause_Click(object sender, RoutedEventArgs e)
        {
            if (graph.State != FilterState.Paused)
                graph.PauseAsync();
            else
                graph.RunAsync();
        }

        private void Play_Click(object sender, RoutedEventArgs e)
        {
            if (!CaptureDeviceConfiguration.RequestDeviceAccess())
            {
                MessageBox.Show("You must allow access to device");
                return;
            }

            CaptureSource source = new CaptureSource();
            source.AudioCaptureDevice = CaptureDeviceConfiguration.GetDefaultAudioCaptureDevice();
            source.VideoCaptureDevice = null;

            sourceFilter.LoadAsync(source);
        }

        private void File_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Wave file (*.wav)|*.wav";

            if (saveFileDialog.ShowDialog().GetValueOrDefault())
            {
                fileWriter.FileStream = saveFileDialog.OpenFile();
                playButton.IsEnabled = true;
            }
        }
    }
}
